package com.maijinjie.admin.content.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.maijinjie.common.bean.Pagination;
import com.maijinjie.common.bean.StatusBean;
import com.maijinjie.admin.content.bean.TableDownloadInfoBean;
import com.maijinjie.admin.content.dao.TableDownloadInfoMapper;
import com.maijinjie.admin.content.field.TableDownloadInfoFields;
import com.maijinjie.admin.content.service.TableDownloadInfoService;
import com.maijinjie.common.service.impl.BaseServiceImpl;

/**
 * TableDownloadInfoServiceImpl
 * @author 罗正杰
 **/
@Service("tableDownloadInfoService")
public class TableDownloadInfoServiceImpl extends BaseServiceImpl implements TableDownloadInfoService{
	@Resource
	TableDownloadInfoMapper tableDownloadInfoMapper;
	
	@Override
	public TableDownloadInfoBean getBeanById(Integer id) throws Exception{
		if(null==id || 0>=id){
			return null;
		} else {
			return (TableDownloadInfoBean) getBeanById(tableDownloadInfoMapper, id);
		}
	}
	@Override
	public int count(Map<String, Object> params) throws Exception {
		return tableDownloadInfoMapper.count(params);
	}
	@Override
	public List<TableDownloadInfoBean> list(Map<String, Object> params) throws Exception {
		return (List<TableDownloadInfoBean>) tableDownloadInfoMapper.list(params);
	}
	@Override
	public StatusBean save(TableDownloadInfoBean bean, String createBy, Integer createUserId) throws Exception {
		Date currDate = new Date();
		//TODO 处理业务
		bean.setCreateUserId(createUserId);
		bean.setCreateDate(currDate);
		if(0<tableDownloadInfoMapper.save(bean)){
			return success("操作成功！");
		}
		return error("操作失败！");
	}
	@Override
	public StatusBean update(TableDownloadInfoBean bean, String createBy, Integer createUserId) throws Exception {
		if(null!=bean){
			Date currDate = new Date();
			HashMap<String, Object> params = new HashMap<String, Object>();
			//TODO 处理业务
			
			if(tableDownloadInfoMapper.update(params)){
				return success("操作成功！");
			}
		}
		return error("操作失败！");
	}
	@Override
	public StatusBean deleteById(Integer id) throws Exception {
		if(null!=id && 0<id){
			HashMap<String, Object> params = new HashMap<String, Object>();
			params.put(TableDownloadInfoFields.id.toString(), id);
			if(tableDownloadInfoMapper.delete(params)){
				return success("操作成功！");
			}
		}
		return error("操作失败！");
	}
	@Override
	public Pagination<TableDownloadInfoBean> list(Pagination<TableDownloadInfoBean> pagination) throws Exception {
		return (Pagination<TableDownloadInfoBean>) getPagination(tableDownloadInfoMapper, pagination);
	}
	@Override
	public StatusBean batchDelete(String ids) throws Exception {
		if(null!=ids && !"".equals(ids.trim())) {
			HashMap<String, Object> params = new HashMap<String, Object>();
			params.put(TableDownloadInfoFields.id.toString(), ids);
			if(tableDownloadInfoMapper.batchDelete(params)){
				return success("操作成功！");
			}
		}
		return error("操作失败！");
	}
	@Override
	public List<TableDownloadInfoBean> listAll() throws Exception {
		return (List<TableDownloadInfoBean>) tableDownloadInfoMapper.list(null);
	}
	@Override
	public Map<String, List> listFileByCategory(Map<String, Object> params) throws Exception {
		Map<String,List> result = new HashMap<String,List>();
		List<TableDownloadInfoBean> doclist = new ArrayList<TableDownloadInfoBean>();
		List<TableDownloadInfoBean> pdflist = new ArrayList<TableDownloadInfoBean>();
		List<TableDownloadInfoBean> xlslist = new ArrayList<TableDownloadInfoBean>();
		result.put("doc", doclist);
		result.put("pdf", pdflist);
		result.put("xls", xlslist);
		
		params.put("endfix", "doc");
		doclist.addAll((List) this.tableDownloadInfoMapper.list(params));
		params.put("endfix", "docx");
		doclist.addAll((List) this.tableDownloadInfoMapper.list(params));
		params.put("endfix", "xls");
		xlslist.addAll((List) this.tableDownloadInfoMapper.list(params));
		params.put("endfix", "xlsx");
		xlslist.addAll((List) this.tableDownloadInfoMapper.list(params));
		params.put("endfix", "pdf");
		pdflist.addAll((List) this.tableDownloadInfoMapper.list(params));
		return result;
	}
}
